<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Labor;
use App\Services\LaborService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LaborController extends Controller
{
    protected $LaborService;
    public function __construct(LaborService $LaborService)
    {
        $this->LaborService = $LaborService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->title;
        $team_id = $request->team_id;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $sql = $this->LaborService->get($title);
        $sql->where('company_id',$request->company_id);
        // $sql->when($team_id, function ($query, $team_id) {
        //     return $query->where('team_id', $team_id);
        // });
        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]->registrant = '';
            if($v->admin){
                $data[$k]->registrant = $v->admin->name;
            }
        }
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function all(Request $request)
    {
        $title = $request->title;
        $sql = $this->LaborService->get($title);
        $sql->where('company_id',$request->company_id);
        $data = $sql->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function salarySummary(Request $request)
    {
        $page = $request->page ?: 1;
        $pageSize = $request->pageSize ?: 10;
        $page = $page * $pageSize - $pageSize;
        $sql = $this->LaborService->get('');
        $sql->select('id','photo','mobile','name');
        $sql->with('LaborAttendances');
        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        $year = Date('Y',time());
        foreach ($data as $k => $v) {
            $data[$k]->m1 = $this->getHour($v->LaborAttendances,$year,'01');
            $data[$k]->m2 = $this->getHour($v->LaborAttendances,$year,'02');
            $data[$k]->m3 = $this->getHour($v->LaborAttendances,$year,'03');
            $data[$k]->m4 = $this->getHour($v->LaborAttendances,$year,'04');
            $data[$k]->m5 = $this->getHour($v->LaborAttendances,$year,'05');
            $data[$k]->m6 = $this->getHour($v->LaborAttendances,$year,'06');
            $data[$k]->m7 = $this->getHour($v->LaborAttendances,$year,'07');
            $data[$k]->m8 = $this->getHour($v->LaborAttendances,$year,'08');
            $data[$k]->m9 = $this->getHour($v->LaborAttendances,$year,'09');
            $data[$k]->m10 = $this->getHour($v->LaborAttendances,$year,'10');
            $data[$k]->m11 = $this->getHour($v->LaborAttendances,$year,'11');
            $data[$k]->m12 = $this->getHour($v->LaborAttendances,$year,'12');
            $data[$k]->days = $data[$k]->m1 + $data[$k]->m2 + $data[$k]->m3 + $data[$k]->m4 + $data[$k]->m5 + $data[$k]->m6 + $data[$k]->m7 + $data[$k]->m8 + $data[$k]->m9 + $data[$k]->m10 + $data[$k]->m11 + $data[$k]->m12;

        }
        //根据考勤计算工资
        return response()->json(['code' => 200, 'message' => '操作成功','year'=>$year,'total'=>$total,'data'=>$data]);
    }


    public function getHour($data,$y,$m)
    {
        $res = 0;
        foreach ($data as $key => $value) {
            if(strpos($value->attendance_date, $y.'-'.$m) !== false){
                if($value->work_hours == 9){
                    $res += 1;
                }else{
                    $res += 0.5;
                }
            }
        }
        return $res;
    }



    public function show(Request $request)
    {
        $data = Labor::find($request->id);
        if($data->birth){
            $data->birth = strtotime($data->birth) * 1000;
        }
        $resources = $data->resources;
        $third_eduUrl = [];
        $paper_fileUrl = [];
        foreach ($resources as $k => $v) {
            $arr = [];
            $arr['id'] = $v->id;
            $arr['name'] = $v->resource->name;
            $arr['url'] = $v->resource->url;
            if($data->third_edu){
                $third_edu = explode(',',$data->third_edu);
                if(in_array($v->resource_id,$third_edu)){
                    $third_eduUrl[] = $arr;
                }
            }

            $paper_file_arr = [];
            $paper_file_arr['id'] = $v->id;
            $paper_file_arr['name'] = $v->resource->name;
            $paper_file_arr['url'] = $v->resource->url;
            if($data->paper_file){
                $paper_file = explode(',',$data->paper_file);
                if(in_array($v->resource_id,$paper_file)){
                    $paper_fileUrl[] = $paper_file_arr;
                }
            }

        }
        $data->third_eduUrl = $third_eduUrl;
        $data->paper_fileUrl = $paper_fileUrl;
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $POSTONLY = Labor::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['admin_id'] = Auth::user()->id;
        $input['company_id'] = $request->company_id;
        $data = $this->LaborService->insert($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function storeBatch(Request $request)
    {
        $input['labors'] = $request->labors;
        $input['admin_id'] = Auth::user()->id;
        $input['company_id'] = $request->company_id;
        $data = $this->LaborService->insertBatch($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $POSTONLY = Labor::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['id'] = $request->id;
        $input['admin_id'] = Auth::user()->id;
        $input['company_id'] = $request->company_id;
        $data = $this->LaborService->update($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        if(!$id){
            return response()->json(['code' => 404, 'message' => '请选择要操作的记录']);
        }
        $data = Labor::find($id);
        $data->delete();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }



}
